#!/usr/bin/env python
# encoding: utf-8
# @author: qiaowei
# @license: (C) Copyright 2013-2019
# @contact: weiweiqiao@126.com
# @file: __table_model.py
# @time: 2020/6/19
# @desc:


import PySide2
import typing
from PySide2.QtCore import QAbstractTableModel
from PySide2.QtCore import Qt
from PySide2.QtGui import QFont
from PySide2.QtGui import QBrush
from PySide2.QtGui import QColor


class TableModel(QAbstractTableModel):
    def __init__(self, parent: PySide2.QtCore.QObject = None):
        """
        初始化模型，模型保存数据为列表，初始状态为空列表
        :param parent: parent
        """
        super().__init__(parent)
        self.__informations = []
        # self.__informations.append(Information())

    def rowCount(self, parent: PySide2.QtCore.QModelIndex = ...) -> int:
        """
        返回模型要显示数据的行数，即数据个数
        :param parent: 项索引
        :return: 要显示的数据的行数
        """
        return len(self.__informations)

    def columnCount(self, parent: PySide2.QtCore.QModelIndex = ...) -> int:
        """
        返回模型要显示数据的行数，即每个数据含有的子数据个数
        :param parent: 项索引
        :return: 每个数据包含的子数据个数
        """
        return 4

    def data(self, index: PySide2.QtCore.QModelIndex, role: int = ...) -> typing.Any:
        """
        根据角色role，向view返回不同的值
        :param index: 索引项
        :param role: 角色
        :return: 在索引index处，不同角色role对应的值
        """
        var = False

        if Qt.DisplayRole == role:
            if 0 != len(self.__informations):
                if 0 == index.column():
                    return self.__informations[index.row()].sector_information
                elif 1 == index.column():
                    return self.__informations[index.row()].frequency
                elif 2 == index.column():
                    return self.__informations[index.row()].inter_modulation
                elif 3 == index.column():
                    return self.__informations[index.row()].inter_modulation_frequency

        # 设置项的字体
        if Qt.FontRole == role:
            if self.__informations[index.row()].inter_modulation:
                return QFont("Courier New", 14, QFont.Bold)
            else:
                return QFont("Courier New", 10, QFont.Bold)

        # 设置项的背景色
        if Qt.BackgroundColorRole == role:
            i = index.row() % 2

            if 0 == i:
                # 天蓝色
                return QColor(135, 206, 255)
            elif 1 == i:
                # 黄色
                return QColor(255, 255, 0)
            # elif 2 == i:
            #     return QColor(238, 162, 173)

        if Qt.ForegroundRole == role:
            if self.__informations[index.row()].inter_modulation:
                return QBrush(Qt.red, Qt.SolidPattern)
            else:
                return QBrush(Qt.black, Qt.SolidPattern)

        # 取消注释导致项出现复选框
        # return typing.Any

    def headerData(self,
                   section: int,
                   orientation: PySide2.QtCore.Qt.Orientation,
                   role: int = ...) -> typing.Any:
        """
        设置表头显示内容
        :param section: 表头的行号/列号
        :param orientation: 水平表头/垂直表头
        :param role: 表头的角色
        :return: 表头要显示的内容，可以是任何类型
        """
        # 设置水平表头进行
        if Qt.Horizontal == orientation:
            if Qt.DisplayRole == role:
                if 0 == section:
                    return '扇区信息'
                elif 1 == section:
                    return '频率(MHz)'
                elif 2 == section:
                    return '是否互调'
                elif 3 == section:
                    return '互调频率(MHz)'

            # 设置表头字体
            if Qt.FontRole == role:
                return QFont("Courier New", 10, QFont.Bold)

        # Qt.Vertical情况下调用父类headerData方法
        return super().headerData(section, orientation, role)

        # return typing.Any

    def set_model(self, informations: list):
        """
        根据传入的列表数据源设置模型，在设置前/后，重置模型，确保数据更新
        :param informations: 模型要调用的列表信息
        """
        self.beginResetModel()
        self.__informations = informations
        self.endResetModel()

    @property
    def informations(self):
        return self.__informations
